package com.example.test_witns.utils;

import android.content.Context;
import android.graphics.Bitmap;

import androidx.annotation.DrawableRes;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;

public final class ImageLoaderUtils {
    private ImageLoaderUtils() {
    }

    // 图片加载中
    private static int sImageLoadingRes = 0;
    // 图片地址异常
    private static int sImageUriErrorRes = 0;
    // 图片 ( 加载 / 解码 ) 失败
    private static int sImageFailRes = 0;
    // 图片默认加载配置
    private static final DisplayImageOptions DF_OPTIONS = defaultOptions();

    /**
     * 初始化 ImageLoader 加载配置
     * @param context {@link Context}
     */
    public static void init(final Context context) {
        DisplayImageOptions options = DF_OPTIONS;
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .defaultDisplayImageOptions(options)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCacheSize(2 * 1024 * 1024)
                .memoryCacheSizePercentage(13)
                .diskCacheSize(50 * 1024 * 1024)
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .imageDownloader(new BaseImageDownloader(context))
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .build();
        ImageLoader.getInstance().init(config);
    }

    /**
     * 获取 DisplayImageOptions 图片加载配置
     * @return {@link DisplayImageOptions}
     */
    public static DisplayImageOptions defaultOptions() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(sImageLoadingRes)
                .showImageForEmptyUri(sImageUriErrorRes)
                .showImageOnFail(sImageFailRes)
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .cacheInMemory(true)
                .considerExifParams(false)
                .resetViewBeforeLoading(false)
                .cacheOnDisk(true)
                .build();
        return options;
    }

    /**
     * 获取图片默认加载配置
     * @param loadingRes 设置加载中显示的图片
     * @return {@link DisplayImageOptions}
     */
    public static DisplayImageOptions getDefaultImageOptions(@DrawableRes final int loadingRes) {
        DisplayImageOptions.Builder optionsBuilder = cloneImageOptions(DF_OPTIONS);
        optionsBuilder.showImageOnLoading(loadingRes)
                .showImageForEmptyUri(loadingRes)
                .showImageOnFail(loadingRes);
        return optionsBuilder.build();
    }

    /**
     * 获取不使用缓存的图片加载配置
     * @return {@link DisplayImageOptions}
     */
    public static DisplayImageOptions getNoCacheImageOptions() {
        return getCacheImageOptions(false, false, -1);
    }

    /**
     * 获取不使用缓存的图片加载配置
     * @param loadingRes 设置加载中显示的图片
     * @return {@link DisplayImageOptions}
     */
    public static DisplayImageOptions getNoCacheImageOptions(@DrawableRes final int loadingRes) {
        return getCacheImageOptions(false, false, loadingRes);
    }

    /**
     * 获取 ImageLoader 图片缓存加载配置
     * @param isCache     是否缓存在内存中
     * @param isCacheDisk 是否保存在 SDCard
     * @return {@link DisplayImageOptions}
     */
    public static DisplayImageOptions getCacheImageOptions(final boolean isCache, final boolean isCacheDisk) {
        return getCacheImageOptions(isCache, isCacheDisk, -1);
    }

    /**
     * 获取 ImageLoader 图片加载配置
     * @param isCache     是否缓存在内存中
     * @param isCacheDisk 是否保存在 SDCard
     * @param loadingRes  设置加载中显示的图片
     * @return {@link DisplayImageOptions}
     */
    public static DisplayImageOptions getCacheImageOptions(final boolean isCache,
                                                           final boolean isCacheDisk,
                                                           @DrawableRes final int loadingRes) {
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder()
                .imageScaleType(ImageScaleType.EXACTLY)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .cacheInMemory(isCache)
                .displayer(new RoundedBitmapDisplayer(20))
                .cacheOnDisk(isCacheDisk);
        if (loadingRes != 0) {
            builder.showImageOnLoading(loadingRes)
                    .showImageForEmptyUri(loadingRes)
                    .showImageOnFail(loadingRes);
        }
        return builder.build();
    }

    /**
     * 克隆图片加载配置
     * @param options 待克隆加载配置
     * @return {@link DisplayImageOptions}
     */
    public static DisplayImageOptions.Builder cloneImageOptions(final DisplayImageOptions options) {
        return (options != null) ? new DisplayImageOptions.Builder().cloneFrom(options) : null;
    }

    /**
     * 获取圆角图片加载配置
     * @param cornerRadiusPixels 圆角大小
     * @return {@link DisplayImageOptions}
     */
    public static DisplayImageOptions getRoundedBitmapDisplayer(final int cornerRadiusPixels) {
        return getBitmapDisplayerOptions(cloneImageOptions(DF_OPTIONS).build(), new RoundedBitmapDisplayer(cornerRadiusPixels));
    }

    /**
     * 获取圆角图片加载配置
     * @param options            {@link DisplayImageOptions}
     * @param cornerRadiusPixels 圆角大小
     * @return {@link DisplayImageOptions}
     */
    public static DisplayImageOptions getRoundedBitmapDisplayer(final DisplayImageOptions options, final int cornerRadiusPixels) {
        if (options != null) {
            return getBitmapDisplayerOptions(options, new RoundedBitmapDisplayer(cornerRadiusPixels));
        }
        return options;
    }

    /**
     * 获取图片效果加载配置
     * @param options   {@link DisplayImageOptions}
     * @return {@link DisplayImageOptions}
     */
    public static DisplayImageOptions getBitmapDisplayerOptions(DisplayImageOptions options, RoundedBitmapDisplayer displayer) {
        if (options != null) {
            DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder().cloneFrom(options);
            builder.displayer(displayer);
            return builder.build();
        }
        return options;
    }
}